#!/bin/bash
set -ex
cd "$(dirname "$0")/"

# This is part of a hack to get dependencies needed for the TTS Rust server, because it integrates a Python component
[ -f pyproject.toml ] || wget https://raw.githubusercontent.com/kyutai-labs/moshi/a40c5612ade3496f4e4aa47273964404ba287168/rust/moshi-server/pyproject.toml
[ -f uv.lock ] || wget https://raw.githubusercontent.com/kyutai-labs/moshi/a40c5612ade3496f4e4aa47273964404ba287168/rust/moshi-server/uv.lock


# A fix for building Sentencepiece on GCC 15, see: https://github.com/google/sentencepiece/issues/1108
export CXXFLAGS="-include cstdint"

uv venv
source .venv/bin/activate

# This env var must be set to get the correct environment for the Rust build.
# Must be set before running `cargo install`!
# If you don't have it, you'll see an error like `no module named 'huggingface_hub'`
# or similar, which means you don't have the necessary Python packages installed.
export LD_LIBRARY_PATH=$(python -c 'import sysconfig; print(sysconfig.get_config_var("LIBDIR"))')

cd ..

# If you already have moshi-server installed and things are not working because of the LD_LIBRARY_PATH issue,
# you might have to force a rebuild with --force.
cargo install --features cuda moshi-server@0.6.3

# Subtle detail here: We use the full path to `moshi-server` because there is a `moshi-server` binary
# from the `moshi` Python package. We'll fix this conflict soon.
# If you have a non-standard installation, you might need to adjust the path.
uv run --locked --project ./dockerless  ~/.cargo/bin/moshi-server worker --config services/moshi-server/configs/tts.toml --port 8089
